﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Transactions;
namespace BLL
{
    /// <summary>
    /// Summary description for ProductManager
    /// </summary>
    public class ProductManager
    {
        private DAL.EShopDBDataContext db = new DAL.EShopDBDataContext(ConfigurationManager.ConnectionStrings["EShopConnectionString"].ConnectionString);
        public ProductManager()
        {
           
        }

        public void AddProduct(DAL.Product prod)
        {
            //using (DAL.EShopDBDataContext db = new DAL.EShopDBDataContext(ConfigurationManager.ConnectionStrings["EShopConnectionString"].ConnectionString))
            //{
                int newId;
                if (db.Products.Count() == 0)
                    newId = 1;
                else
                {
                    newId = (from c in db.Products
                                 select c.ProductId).Max() + 1;
                }
                prod.ProductId = newId;
                //prod.Url = string.Format("Category.aspx?id={0}", newId);
                db.Products.InsertOnSubmit(prod);
                using (TransactionScope ts = new TransactionScope())
                {
                    db.SubmitChanges();
                    //SiteMapNode node = new SiteMapNode(SiteMap.Provider, newId.ToString(), category.Url, category.Title, category.Description);
                    //SqlSiteMapProvider smp = (SqlSiteMapProvider)SiteMap.CurrentNode.Provider;
                    //node.Roles = smp.GetRolesListIfAnyExist(category);
                    //smp.AddNodeToSiteMap(node, SiteMap.CurrentNode);
                    ts.Complete();
                }
            //}
        }

        public DAL.Product GetProduct(int? productId)
        {

                var product = db.Products.Single(c => c.ProductId == productId);
                return product;

        }
    }
}
